﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1.Work4
{
    public class Person
    {
        public string Name { get; }
        public int Age { get; }


        public Person()
        {
            Name = "Unknown";
            Age = 0;
        }

        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public override string ToString()
        {
            return $"Person: {{ Name = {Name}, Age = {Age} }}";
        }
    }

    class Work43
    {
        static void Main()
        {
            // 1. 获取 Person 类型
            Type personType = typeof(Person);

            ConstructorInfo? ctor = personType.GetConstructor(new Type[] { typeof(string), typeof(int) });
            if (ctor == null)
            {
                Console.WriteLine("未找到匹配的构造函数！");
                return;
            }

            object personInstance = ctor.Invoke(new object[] { "Alice", 30 });
            Console.WriteLine(personInstance);

            object? defaultPerson = Activator.CreateInstance(personType);
            Console.WriteLine(defaultPerson);

            // 5. 也可以通过 Activator 创建带参实例
            object? bob = Activator.CreateInstance(personType, new object[] { "Bob", 25 });
            Console.WriteLine(bob);
        }
    }
}
